package ${packageName}.controller;

import ${packageName}.model.${modelName};
import ${packageName}.service.${serviceName};
import ${packageName}.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import java.util.List;

@RestController
public class ${controllerName} {

    @Autowired
    ${serviceName} ${serviceName?uncap_first};

    @GetMapping("/${modelName?lower_case}s")
    public R getAll${modelName}s(){
        List<${modelName}> ${modelName?lower_case}s = ${serviceName?uncap_first}.getAll${modelName}s();
        return R.ok().data("${modelName?lower_case}s", ${modelName?lower_case}s);
    }

    @DeleteMapping("/${modelName?lower_case}s")
    public R deleteAll${modelName}s() {
        return ${serviceName?uncap_first}.deleteAll${modelName}s() > 0 ? R.ok() : R.error();
    }

    @GetMapping("/${modelName?lower_case}")
    public R get${modelName}ById(<#list primaryKeys as column>${column.type} ${column.propertyName?uncap_first}<#if column_has_next>, </#if></#list>){
        ${modelName} ${modelName?lower_case} = ${serviceName?uncap_first}.get${modelName}ById(<#list primaryKeys as column>${column.propertyName?uncap_first}<#if column_has_next>, </#if></#list>);
        return R.ok().data("${modelName?lower_case}", ${modelName?lower_case});
    }

    @PutMapping("/${modelName?lower_case}")
    public R update${modelName}ById(${modelName} ${modelName?uncap_first}) {
        return ${serviceName?uncap_first}.update${modelName}ById(${modelName?uncap_first}) > 0 ? R.ok() : R.error();
    }

    @PostMapping("/${modelName?lower_case}")
    public R add${modelName}(${modelName} ${modelName?uncap_first}) {
        return ${serviceName?uncap_first}.add${modelName}(${modelName?uncap_first}) > 0 ? R.ok() : R.error();
    }

    @DeleteMapping("/${modelName?lower_case}")
    public R delete${modelName}ById(<#list primaryKeys as column>${column.type} ${column.propertyName?uncap_first}<#if column_has_next>, </#if></#list>){
        return ${serviceName?uncap_first}.delete${modelName}ById(<#list primaryKeys as column>${column.propertyName?uncap_first}<#if column_has_next>, </#if></#list>) > 0 ? R.ok() : R.error();
    }
}